home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / AbstractAction.java next >
Text File  |  1998-06-30  |  5KB  |  162 lines

  1. /*
  2.  * @(#)AbstractAction.java    1.18 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.beans.*;
  25. import java.util.Hashtable;
  26. import java.io.Serializable;
  27.  
  28. /**
  29.  * This class provides default implementations for the JFC Action 
  30.  * interface. Standard behaviors like the get and set methods for
  31.  * Action object properties (icon, text, and enabled) are defined
  32.  * here. The developer need only subclass this abstract class and
  33.  * define the <code>actionPerformed</code> method. 
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate 
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version 1.18 02/02/98
  43.  * @author Georges Saab
  44.  * @see Action
  45.  */
  46. public abstract class AbstractAction implements Action, Cloneable, Serializable 
  47. {
  48.     protected boolean enabled = true;
  49.     // Will be replaced by a lighter weight storage mechanism soon!
  50.     private Hashtable keyTable = new Hashtable(5);
  51.  
  52.     /**
  53.      * Defines an Action object with a default description string
  54.      * and default icon.
  55.      */
  56.     public AbstractAction() {}
  57.     
  58.     /**
  59.      * Defines an Action object with the specified description string
  60.      * and a default icon.
  61.      */
  62.     public AbstractAction(String name) {
  63.     putValue(Action.NAME, name);
  64.     }
  65.  
  66.     /**
  67.      * Defines an Action object with the specified description string
  68.      * and a the specified icon.
  69.      */
  70.     public AbstractAction(String name, Icon icon) {
  71.     this(name);
  72.     putValue(Action.SMALL_ICON, icon);
  73.     }
  74.  
  75.     /** @see Action#getValue */
  76.     // Gets the Object associated with key
  77.     public Object getValue(String key) {
  78.     return keyTable.get(key);
  79.     }
  80.  
  81.     /** @see Action#putValue */
  82.     // Sets the Value associated with key
  83.     public synchronized void putValue(String key, Object newValue) {
  84.     Object oldValue = null;
  85.     if (keyTable.containsKey(key))
  86.         oldValue = keyTable.get(key);
  87.     keyTable.put(key,newValue);
  88.     firePropertyChange(key, oldValue, newValue);
  89.     }
  90.  
  91.     /** @see Action#isEnabled */
  92.     public boolean isEnabled() {
  93.     return enabled;
  94.     }
  95.  
  96.     /** @see Action#setEnabled */
  97.     public synchronized void setEnabled(boolean newValue) {
  98.     boolean oldValue = this.enabled;
  99.     this.enabled = newValue;
  100.     firePropertyChange("enabled", 
  101.                new Boolean(oldValue), new Boolean(newValue));
  102.     }
  103.  
  104.     /*
  105.      * If any PropertyChangeListeners have been registered, the
  106.      * changeSupport field describes them.
  107.      */
  108.     protected java.beans.PropertyChangeSupport changeSupport;
  109.  
  110.     /**
  111.      * Support for reporting bound property changes.  This method can be called
  112.      * when a bound property has changed and it will send the appropriate
  113.      * PropertyChangeEvent to any registered PropertyChangeListeners.
  114.      */
  115.     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  116.         if (changeSupport == null) {
  117.             return;
  118.         }
  119.         changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  120.     }
  121.  
  122.  
  123.     /**
  124.      * Add a PropertyChangeListener to the listener list.
  125.      * The listener is registered for all properties.
  126.      * <p>
  127.      * A PropertyChangeEvent will get fired in response to setting
  128.      * a bound property, e.g. setFont, setBackground, or setForeground.
  129.      * Note that if the current component is inheriting its foreground, 
  130.      * background, or font from its container, then no event will be 
  131.      * fired in response to a change in the inherited property.
  132.      *
  133.      * @param listener  The PropertyChangeListener to be added
  134.      *
  135.      * @see Action#addPropertyChangeListener 
  136.      */
  137.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
  138.         if (changeSupport == null) {
  139.         changeSupport = new java.beans.PropertyChangeSupport(this);
  140.         }
  141.         changeSupport.addPropertyChangeListener(listener);
  142.     }
  143.  
  144.  
  145.     /**
  146.      * Remove a PropertyChangeListener from the listener list.
  147.      * This removes a PropertyChangeListener that was registered
  148.      * for all properties.
  149.      *
  150.      * @param listener  The PropertyChangeListener to be removed
  151.      *
  152.      * @see Action#removePropertyChangeListener 
  153.      */
  154.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
  155.         if (changeSupport == null) {
  156.             return;
  157.         }
  158.         changeSupport.removePropertyChangeListener(listener);
  159.     }
  160.  
  161. }
  162.